package ar.com.larctest.forms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import ar.com.larctest.R;
import ar.com.larctest.adapters.ListViewMeasurementDataAdapter;
import ar.com.larctest.dbHelper.SQLLARCTESTHelper;
import ar.com.larctest.entities.temp.TMeasurementJoin;
import ar.com.larctest.providers.MeasurementProvider;

public class MeasurementSearchActivity extends Activity {
    private static final String TAG = "MeasurementSearchActivity";

    private ListViewMeasurementDataAdapter measurementDataAdapter;
    private List<TMeasurementJoin> lstMeasurementData = new ArrayList<TMeasurementJoin>();
    TMeasurementJoin selectedMeasurementItem;

    DatePicker dpDateMeasurement;
    ListView listViewMeasurementData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_measurement_search);

        listViewMeasurementData = (ListView) findViewById(R.id.lvMeasurementData);
        dpDateMeasurement = (DatePicker) findViewById(R.id.dpMeasurement);

        try
        {
            measurementDataAdapter = new ListViewMeasurementDataAdapter(MeasurementSearchActivity.this, R.id.lvHorses, lstMeasurementData);
            View header = getLayoutInflater().inflate(R.layout.listview_header_measurement, null);
            listViewMeasurementData.addHeaderView(header);

            listViewMeasurementData.setAdapter(measurementDataAdapter);
            listViewMeasurementData.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    for(TMeasurementJoin measurementItem: lstMeasurementData)
                        measurementItem.setSelected(false);

                    selectedMeasurementItem = lstMeasurementData.get(position - 1);
                    selectedMeasurementItem.setSelected(true);
                    measurementDataAdapter.notifyDataSetChanged();
                }
            });
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    public void onClickNew(View view) {
        Intent intentMeasurementNew = new Intent(this, MeasurementInitActivity.class);
        startActivityForResult(intentMeasurementNew, 1);

    }

    public void onClickView(View view) {

    }

    public void onClickSearch(View view)
    {
        SQLLARCTESTHelper sqlHelper = SQLLARCTESTHelper.getHelper(this);
        sqlHelper.dbOpen();

        try
        {
            String dateTime = dpDateMeasurement.getDayOfMonth() + "/" + (dpDateMeasurement.getMonth() + 1) + "/" + dpDateMeasurement.getYear();

            MeasurementProvider measurementProvider = new MeasurementProvider(this);
            lstMeasurementData = measurementProvider.getMeasurementsJoinItems(dateTime);
            measurementDataAdapter.clear();
            measurementDataAdapter.notifyDataSetChanged();
            measurementDataAdapter = new ListViewMeasurementDataAdapter(MeasurementSearchActivity.this, R.id.lvHorses, lstMeasurementData);
            listViewMeasurementData.setAdapter(measurementDataAdapter);
            measurementDataAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
        finally {
            sqlHelper.dbClose();
        }
    }
}
